<link rel="stylesheet" href="/packages/resource/pintuer/pintuer.css">

<?php
    @Model()->query("alter table `ra_proxy` add COLUMN  `is_special` int(1) default 0");
?>
<div class="pintuer">
    <div class="padding">
        <h5 class="text-yellow">
            微分销会员管理
        </h5>

        <hr class="bg-yellow">


        <div class="line margin-top">
            <form action="/admin" class="form-inline">
                <input type="hidden" name="act" value="proxy_order">
                <input type="hidden" name="op" value="we_distribute_member">

                <div class="form-group">
                    <div class="field">
                        <div class="input-group">
                            <span class="addon">
                                开始时间：
                            </span>
                            <input type="date" class="input" name="start_time"
                                   value="<?php echo $_GET['start_time'] ?>">
                        </div>
                        <div class="input-group">
                            <span class="addon">
                                开始时间：
                            </span>
                            <input type="date" class="input" name="end_time" value="<?php echo $_GET['end_time'] ?>">
                        </div>

                        <div class="input-group">
                            <span class="addon">会员名</span>
                            <input type="text" name="member_name" class="input"
                                   value="<?php echo $_GET['buyer_name'] ?>">
                        </div>
                        <!--                        <div class="input-group">-->
                        <!--                            <span class="addon">收益人ID</span>-->
                        <!--                            <input type="text" name="get_name" class="input" value="-->
                        <?php //echo $_GET['get_name'] ?><!--">-->
                        <!--                        </div>-->

                        <div class="button-group">
                            <button type="submit" class="button bg-blue">检索</button>

                        </div>
                    </div>
                </div>

            </form>


            <hr>

            <?php
                if (isset($_GET['parent_id'])) {
                    $member = Model('member')->find($_GET['parent_id']);
                    echo $member['member_name'] . ' 的下级会员';
                }
            ?>
            <style>
                table td{
                    padding-top: 10px !important;
                    padding-bottom: 10px !important;
                }
            </style>

            <div class="alert alert-blue">
                特殊分销商使用单独的分销商提成比例，并不能发展下级分销商（买家设置该项无效）
            </div>

            <table class="table table-bordered margin-top">
                <tr class="thead">
                    <th>
                        ID
                    </th>
                    <th>会员名</th>
                    <th>上级会员</th>
                    <th>
                        推荐人(点击修改)
                    </th>
                    <th>成为分销会员时间</th>
                    <th>等级</th>
                    <th>身份类型</th>
                    <th>
                        特殊分销商
                    </th>
                    <th>操作</th>
                </tr>
                <?php
                    $m = Model()->table('proxy');
                    $condition = array();
                    if (!empty($_GET['start_time'])) {
                        $condition['time'] = array(
                            'gt',
                            strtotime($_GET['start_time'])
                        );
                    }

                    if (!empty($_GET['end_time'])) {
                        $condition['time'] = array(
                            'lt',
                            strtotime($_GET['end_time'])
                        );
                    }

                    if (!empty($_GET['member_name'])) {
                        $member = null;
                        $member = Model('member')->where(['member_name' => $_GET['member_name']])->find();
                        if ($member) {
                            $condition['member_id'] = $member['member_id'];
                        }

                    }

                    if (!empty($_GET['parent_id'])) {
                        $condition['parent_id'] = $_GET['parent_id'];
                    }


                    $list = $m->where($condition)->page(30)->select();
                    //                    if(!empty($_GET[]))
                    if ($list) {
                        foreach ($list as $item) {
                            $member = Model('member')->find($item['member_id']);

                            if (empty($member)) {
                                continue;
                            }
                            ?>
                            <tr>
                                <td>
                                    <?php
                                        echo $member['member_id'];
                                    ?>
                                </td>
                                <td>
                                    <?php
                                        if (empty($member)) {
                                            echo "该会员已被删除";
                                        } else {
                                            ?>
                                            <a target="_blank"
                                               href="/admin/index.php?act=member&op=member_edit&member_id=<?php echo $member['member_id'] ?>">
                                                <?php
                                                    echo $member['member_name'];
                                                ?>
                                            </a>
                                            <?php

                                        }
                                    ?>
                                </td>
                                <td>
                                    <?php
                                        if ($item['parent_id'] == 0) {
                                            echo "金牌代理";
                                        }
                                        else{
                                            $mem = ml('member')->find($item['parent_id']);

                                            ?>
                                            <a target="_blank"
                                               href="/admin/index.php?act=member&op=member_edit&member_id=<?php echo $mem['member_id'] ?>">
                                                <?php
                                                    echo $mem['member_name'];
                                                ?>
                                            </a>
                                            <?php
                                        }

                                    ?>
                                </td>
                                <td>
                                    <a href="javascript:;" data-id="<?php echo $item['member_id']?>" class="modify_inviter">
                                    <?php
                                        $inviter = ml("proxy_pre")->find([
                                            'openid' => $member['wechat_openid']
                                        ]);
                                        if ($inviter) {
                                            $inviter = ml('member')->find([
                                                'member_id' => $inviter['parent_id'],

                                            ]);
                                            if ($inviter) {
                                                echo $inviter['member_name'];
                                            }
                                            else{
                                                echo "无";
                                            }
                                        }
                                        else{
                                            echo "无";
                                        }
                                    ?>
                                    </a>
                                </td>
                                <td>
                                    <?php
                                        echo date('Y-m-d H:i:s', $item['time']);
                                    ?>
                                </td>
                                <td>
                                    <?php echo (new Distribute($member['member_id']))->getLevel() ?>
                                </td>
                                <td>
                                    <?php
                                        echo !$item['is_buyer'] ? "<font color='#00bfff'>分销商</font>" : "<font color='red'>买家</font>";
                                    ?>
                                </td>

                                <td>
                                    <input type="checkbox" data-id="<?php echo $item['member_id']?>" <?php if($item['is_special']) echo 'checked' ?>>
                                </td>

                                <td>
                                    <?php
                                        if ($item['is_buyer'] != 1) {

                                            ?>
                                            <a href="/admin/index.php?act=proxy_order&op=we_distribute_member&parent_id=<?php echo $member['member_id'] ?>">查看下级会员</a>
                                            <span class="padding">
                                                |
                                            </span>
                                            <a href="javascript:;" class="give_buyer" data-id="<?php echo $item['member_id']?>">
                                                添加下级
                                            </a>
                                            <span class="padding">
                                                |
                                            </span>
                                            <?php
                                        }
                                    ?>
                                    <!--                                    <a href="javascript:;" data-mem-id='--><?php //echo $member['member_id']?><!--'>修改上级分销商</a>-->
                                    <!--                                    <span class="padding">-->
                                    <!--                                    |-->
                                    <!--                                    </span>-->
                                    <a href="/admin/index.php?act=proxy_order&op=we_distribute&get_name=<?php echo $member['member_name'] ?>">
                                        查看订单
                                    </a>
                                    <span class="padding">
                                        |
                                    </span>
                                    <a href="/admin/index.php?act=proxy_order&op=cancel_distribute&member_id=<?php echo $item['member_id']?>" onclick="return confirm('一经取消不可恢复，该会员的下级会员会自动添加到该会员的上级')">取消分销</a>
                                </td>


                            </tr>
                            <?php
                        }

                    } else {

                    }

                ?>
            </table>

            <link rel="stylesheet" href="/packages/resource/css/pager.css">
            <div class="_pager margin-big-top">
                <?php
                    echo $m->showpage();
                ?>
            </div>
        </div>
    </div>


</div>

<div class="point_proxy" style="display: none;">
    <div class="pintuer" style="max-width: 400px;padding:20px">
        <div class="form-group">
            <div class="label"></div>
            <div class="field">
                <div class="input-group">
                    <span class="addon">上级分销商名称</span>
                    <input type="text" class="input" name="parent_proxy">
                </div>
            </div>
        </div>
        <div class="alert alert-blue">
            提示：不符合条件的操作不会生效
            <br>
            1. 你不能将一个分销商指向比他还低的层级
            <br>
            2. 你不能将以个分销商指定为一个不是分销商的下级
        </div>
        <div class="text-center margin-big-top">
            <a href="javascript:;" class="button bg-blue confirm">确定</a>
        </div>

    </div>
</div>


<script src="/data/resource/layer/layer.js"></script>
<script>

    $('.give_buyer').click(function () {
        sel_id = $(this).attr('data-id');
        layer.open({
            type: 2,
            title : "选择",
            content: '/admin/?act=proxy_order&op=proxy_sel_member',
            area : ['800px','800px' ]
        });

    })

    var sel_id = null;

    $('a[data-mem-id]').click(function () {
        sel_id = $(this).attr('data-mem-id');
        layer.open({
            title : "修改上级分销商",
            type : 1,
            content : $('.point_proxy')
        });
    });

    $('.confirm').click(function () {
        if(sel_id == null) {
            layer.msg("错误的操作！");
            return;
        }

        var index = layer.load();
        $.get('/admin/?act=proxy_order&op=point_parent_proxy',{member_name : $('input[name=parent_proxy]').val(),member_id : sel_id}, function (msg) {
            layer.close(index);
            if(msg == 'success'){
                layer.msg("改变成功", function () {
                    location.reload();
                })
            }
            else{
                layer.msg("改变失败！");
            }

        });
    });


    $('input[type=checkbox]').click(function(){
        var member_id = $(this).attr('data-id');
        $.get('/admin/?act=proxy_order&op=change_is_special',{member_id : member_id,value : this.checked ? 1 : 0},function(){

        })
    });


    $('.modify_inviter').click(function(){
        var _this = $(this);
        var id = $(this).attr('data-id');
        var username = prompt("请输入推荐人的ID");
        if(username == "" || username == null) {
            return;
        }
        var index = layer.load();
        $.get('/admin/?act=proxy_order&op=change_inviter',{
            id : id,
            username : username
        },function(msg){
            layer.close(index);
            if(msg != 'success'){
                layer.msg("修改失败");
            }
            else{
                _this.text(username);
            }

        });
    });

</script>

